package com.crm.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.PowerandroleMapper;
import com.crm.model.mdao.RoleMapper;
import com.crm.model.mpojo.Powerandrole;
import com.crm.model.mpojo.Role;
import com.crm.services.PowerService;
import com.crm.services.RoleServices;
import com.crm.util.MapmenusUtil;
import com.github.pagehelper.PageInfo;


@Controller
public class RoleController {
	@Autowired
	RoleServices roleServices;
	
	@Autowired
	PowerService powerService;
	
	@Autowired
	RoleMapper roleMapper;
	
	@Autowired
	PowerandroleMapper powerandroleMapper;
	
	@RequestMapping("/authority/role")
	public ModelAndView role(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("/authority/role");
		List<Map<String,String>> selectAllTable = roleServices.selectByPager(pageNum, pageSize,statusid,search);
		String jsonString = JSONObject.toJSONString(selectAllTable);
		//获取分页信息
		PageInfo<Map<String,String>> pager = new PageInfo<Map<String,String>>(selectAllTable);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		//获取所有菜单
		List<MapmenusUtil> menusList = powerService.menusListAll();
		mv.addObject("menusList", JSONObject.toJSONString(menusList));
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);		
		return mv;
	}
	
	/**
	 * 分页查询角色
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/authority/role")
	@ResponseBody	
	public PageInfo<Map<String, String>> user(Integer pageNum, Integer pageSize,Integer statusid  ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = roleServices.selectByPager(pageNum, pageSize,statusid,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}

	/**
	 * 添加角色页面数据
	 * @return
	 */
	@RequestMapping("/authority/addRoleview")
	@ResponseBody
	public JSONObject addDeptview() {
		JSONObject jsonarray = new JSONObject();
		return jsonarray;
	}
	
	
	/**
	 * 添加角色
	 * @return
	 */
	@RequestMapping("/authority/addRole")
	@ResponseBody
	public JSONObject addRole(@RequestParam(name="roleName",required=false) String roleName,
			@RequestParam(name="roletate",required=false) Integer roleState,
			@RequestParam(name="roleDescript",required=false) String roleDescript,
			@RequestParam(name="menus",required=false) Object menus) {
		JSONObject jsonarray = new JSONObject();
		
		com.crm.model.pojo.Role role=new com.crm.model.pojo.Role();
		role.setRoleName(roleName);
		role.setRoleState(roleState);
		role.setRoleDescript(roleDescript);
		role.setRoleDate(new Date());
		String str= String.valueOf(menus);
		@SuppressWarnings("unchecked")
		List<Integer> limap = JSONArray.parseObject(str,List.class);
		roleServices.save(role,limap);
		return jsonarray;
	}
	
	/**
	 * 修改角色页数据
	 * @return 
	 */
	@PostMapping("/authority/updateRoleview")
	@ResponseBody
	public JSONObject updateRoleview(@RequestParam(name="ID",required=false) Integer id) {
		Role role = roleMapper.selectByPrimaryKey(id);
		List<Powerandrole> selectByRoleId = powerandroleMapper.selectByRoleId(id);
		List<Integer> list=new ArrayList<>();
		for (Powerandrole powerandrole : selectByRoleId) {
			list.add(powerandrole.getpId());
		}
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("Role", role);
		jsonarray.put("menusAll", JSONObject.toJSONString(list));
		return jsonarray;
	}

	
	/**
	 * 修改角色
	 * @return
	 */
	@RequestMapping("/authority/updateRole")
	@ResponseBody
	public JSONObject updateRole(@RequestParam(name="roleId",required=false) Integer roleId,
			@RequestParam(name="roleName",required=false) String roleName,
			@RequestParam(name="roletate",required=false) Integer roleState,
			@RequestParam(name="roleDescript",required=false) String roleDescript,
			@RequestParam(name="menus",required=false) Object menus) {
		JSONObject jsonarray = new JSONObject();
		Role role=new Role();
		role.setRoleId(roleId);
		role.setRoleName(roleName);
		role.setRoleState(roleState);
		role.setRoleDescript(roleDescript);
		String str= String.valueOf(menus);
		@SuppressWarnings("unchecked")
		List<Integer> limap = JSONArray.parseObject(str,List.class);
		roleServices.updateRole(role,limap);
		return jsonarray;
	}
	
	
}
